﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Contoso.ViewModels;

namespace Contoso.Controllers
{
    public class HomeController : Controller
    {
        private SchoolContext db = new SchoolContext();


        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to ASP.NET MVC!";


           SchoolInitializer s = new SchoolInitializer();
          //  s.LoadData(db);
            //var EnrollmentGroupCount = from item in  db.Enrollments.GroupBy(o => o.Student.EnrollmentDate)
            //                           select new 

            var EnroolementGroupCount = from item in db.Students
                                        group item by item.EnrollmentDate into dateGroup
                                        select new EnrollmentDataGroup() {  EnrollmentDate = dateGroup.Key, StudentCount= dateGroup.Count()};
            var query = "SELECT EnrollmentDate, COUNT(*) AS StudentCount "
                            + "FROM Student "
                            + "WHERE EnrollmentDate IS NOT NULL "
                            + "GROUP BY EnrollmentDate";

            var data = db.Database.SqlQuery<EnrollmentDataGroup>(query);
            return View(data);
        }

        public ActionResult About()
        {
            return View();
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}
